package com.aode.register.network;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.aode.common.bean.request.LoginData;
import com.aode.common.bean.response.User;
import com.aode.common.bean.web.Result;
import com.aode.register.service.UserService;
import com.aode.utils.main.RequestUtils;

import java.util.LinkedHashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class HttpData {
         private static MutableLiveData<Result> userMutableLiveData = new MutableLiveData<>();


    /**
     * 登录
     * @param loginData
     * @return
     */
     public static MutableLiveData<Result> register(LoginData loginData){
            Map<String, Object> params = new LinkedHashMap<>();
            params.put("userName", loginData.getUsername());
            params.put("userPassword", loginData.getPassword());
            RequestBody requestBody = RequestUtils.request(params);
            HttpsUtils.getInstance().create(UserService.class)
                .register(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Result userResult) {
                        userMutableLiveData.postValue(userResult);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Result<User> userResult = new Result<>();
                        userResult.setCode("-1");
                        userResult.setMsg(e.getMessage());
                        userMutableLiveData.postValue(userResult);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
                return userMutableLiveData;
            }
}
